<template>
  <div>
    <el-dialog
      :title="info.isAdd ? '管理员添加' : '管理员编辑'"
      :visible.sync="info.isshow"
      @close="close"
    >
      <el-form :model="user">
        {{ user }}
        <el-form-item label=" 所属角色" label-width="120px">
          <el-select v-model="user.roleid">
            <el-option
              v-for="item in rolelist"
              :key="item.id"
              :label="item.rolename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label=" 用户名" label-width="120px">
          <el-input v-model="user.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" label-width="120px">
          <el-input
            v-model="user.password"
            autocomplete="off"
            show-password
          ></el-input>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="user.status"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add"
          >添 加</el-button
        >
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { successAlert } from "../../../utils/alert";
import { formClose } from "../../../minxins/index";
import {
  requseredit,
  requserinfo,
  reqmenulist,
  requseradd,
  reqrolelist,
} from "../../../http/api";  
export default {
  // 接收info
  props: ["info", "arr"],
  data() {
    return {
      // 5.1初始化表单的数据
      user: {
        roleid: "",
        username: "",
        password: "",
        status: 1,
      },
      // 角色列表
      rolelist: [],
    };
  },
  computed: {
    ...mapGetters({
      userInfo:"userInfo"
    }),
  },
  mounted() {
    // 请求角色列表
    reqrolelist().then((res) => {
      if (res.data.code == 200) {
        this.rolelist = res.data.list ? res.data.list : [];
      }
    });
  },
  methods: {
    ...mapActions({
      changeuserInfo:"changeuserInfo"
    }),

    // 清空弹窗的方法
    clearUser() {
      this.user = {
        roleid: "",
        username: "",
        password: "",
        status: 1,
      };
    },
    // 5.4点了添加按钮
    add() {
      requseradd(this.user).then((res) => {
        if (res.data.code == 200) {
          // （1）弹成功
          successAlert(res.data.msg);
          // （2）清空弹窗
          this.clearUser();
          // （3）关闭弹窗
          this.info.isshow = false;
          // （4）添加一条数据，重新请求列表
          this.$emit("init");
        }
      });
    },
  
    // 8.编辑逻辑,获取一条数据
    getOne(uid) {
      // console.log("发请求"+id);
      requserinfo({ uid: uid }).then((res) => {
        if (res.data.code == 200) {
          this.user = res.data.list;    
          // 处理密码（清空密码）
          this.user.password=""
        }
      });
    },
    // 9.修改
    update() {
     
      requseredit(this.user).then((res) => {
        if (res.data.code == 200) {
          // ①弹成功
          successAlert(res.data.msg);
          
          // 如果user.uid==userInfo.uid,清空用户数据，并跳转到登录
          if(this.user.uid===this.userInfo.uid){
            this.changeuserInfo({})
            this.$router.replace("/login")
          }
          // ②弹框内容清空
          this.clearUser();
          // ③弹框消失
          this.info.isshow = false;
          // ④重新请求列表
          this.$emit("init");
        }
      });
    },
  },
  mixins: [formClose],
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
</style>